//
// LBXDownLoadItem.h
// LBXDownLoad
//
// Created by lbxia on 2021/4/20
//
//
    

#import <Foundation/Foundation.h>
#import "LBXDownLoadOperation.h"

// 下载状态
typedef NS_ENUM(NSInteger, LBXDownloadStatus)
{
    LBXDownloadStatus_None = 0,        // 未下载
    LBXDownloadStatus_Readying = 1,    // 等待下载
    LBXDownloadStatus_Running = 2,     // 正在下载
    LBXDownloadStatus_Suspended = 3,   // 下载暂停
    LBXDownloadStatus_Completed = 4,   // 下载完成
    LBXDownloadStatus_Failed = 5,       // 下载失败
};

/**
 *  下载进度相关参数
*/
@interface LBXDownLoadProgress : NSObject

// 续传大小
@property (nonatomic, assign) int64_t resumeBytesWritten;
// 这次写入的数量
@property (nonatomic, assign) int64_t bytesWritten;
// 已下载的数量
@property (nonatomic, assign) int64_t totalBytesWritten;
// 文件的总大小
@property (nonatomic, assign) int64_t totalBytesExpectedToWrite;

//上次返回数据时间
@property (nonatomic, strong) NSDate *preWrittenTime;

// 下载进度百分比
@property (nonatomic, assign) float progress;
// 下载速度,根据
@property (nonatomic, assign) float speed;
// 下载剩余时间s
@property (nonatomic, assign) int64_t remainingTime;

@end


@interface LBXDownLoadItem : NSObject
//下载地址
@property (nonatomic, copy) NSString *remoteURL;

//下载完成后存储路径，存储统一文件夹，根据remoteURL计算生成文件名
@property (nonatomic, copy,readonly) NSString *localFilePath;

//文件大小，单位字节
@property (nonatomic, assign) int64_t fileSize;
//已下载大小
@property (nonatomic, assign) int64_t downSize;

//当前下载状态
@property (nonatomic, assign) LBXDownloadStatus status;
//下载任务标识
@property (nonatomic, assign) int64_t taskIdentifier;

//存储文名
@property (nonatomic, copy) NSString *taskDescription;
//下载顺序优先级，越大优先级越高,默认0
@property (nonatomic, assign) NSInteger priorityLevel;

@property (nonatomic, strong) NSData *resumeData;

#pragma mark- 下载进度回调参数
//下载状态变化block
@property (nonatomic, copy) void (^onStatusChanged)(LBXDownLoadItem* item);

//下载进度变化block
@property (nonatomic, copy) void (^onProgressChanged)(LBXDownLoadItem* item);


//下载操作
@property (nonatomic, strong) LBXDownLoadOperation *op;


//删除当前对象硬盘存储,下载成功和删除下载时调用
- (void)deleteItem;

- (BOOL)fileCacheExist;

//读取沙盒存储信息，返回对象，如果为nil,表示之前没有存储
+ (LBXDownLoadItem*)itemFromSandBoxWithRemoteURL:(NSString*)remoteURL;

+ (BOOL)fileCacheExistWithRemoteURL:(NSString*)remoteURL;

/// 根据下载地址获取本地缓存地址
/// @param remoteURL 如果有值表示已经下载该文件，nil:表示该文件未下载或未下载完成
+ (NSString*)localfilePathWithRemoteURL:(NSString*)remoteURL;

@end
